package com.shop.front.controller.index;

import com.alibaba.fastjson.JSON;
import com.shop.domain.system.Banner;
import com.shop.front.vo.index.IndexVo;
import com.shop.front.vo.index.IntegralIndexVo;
import com.shop.front.vo.index.SearchIndexVo;
import com.shop.front.vo.store.StoreProductVo;
import com.shop.common.enums.System.SystemGroupEnum;
import com.shop.domain.system.SystemGroupData;
import com.shop.front.request.store.StoreProductReq;
import com.shop.front.service.store.StoreProductService;
import com.shop.front.service.system.SystemService;
import com.shop.web.cache.RedisCache;
import com.shop.web.response.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "首页相关", position = -1)
@RestController
@RequestMapping(value = "/index", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
@Slf4j
public class IndexController {

    @Autowired
    private StoreProductService storeProductService;
    @Autowired
    private SystemService systemService;
    @Autowired
    private RedisCache redisCache;


    @ApiOperation(value = "首页")
    @ApiResponses(@ApiResponse(code = 10000, response = IndexVo.class, message = "content"))
    @RequestMapping("/index")
    public BaseResponse index() {
        IndexVo indexVo = null;

        // 首页缓存key
        int cacheTime = 3600;  // 秒
        String cacheKey = "index_index";
        // 缓存是否存在
       /* String cacheVal = redisCache.getString(cacheKey);
        if(StringUtils.isNotBlank(cacheVal)) {
            indexVo = JSON.parseObject(cacheVal, IndexVo.class);
            return new BaseResponse(indexVo);
        }
*/
        indexVo = new IndexVo();

        // 热门商品列表
        List<StoreProductVo> hotList = storeProductService.list(6, StoreProductReq.hotList());
        // 新品商品列表
        List<StoreProductVo> newList = storeProductService.list(6, StoreProductReq.newList());
        // 精品商品列表
        List<StoreProductVo> bestList = storeProductService.list(6, StoreProductReq.bestList());
        // 推荐商品列表
        List<StoreProductVo> goodList = storeProductService.list(6, StoreProductReq.goodList());
        // banner列表
        List<Banner> bannerList = systemService.getBannerList();

        indexVo.setHotList(hotList);
        indexVo.setNewList(newList);
        indexVo.setBestList(bestList);
        indexVo.setGoodList(goodList);
        indexVo.setBannerList(bannerList);

        List<Map<String, String>> rangeList = new ArrayList<>();
        //设置仓库价格区间
        Map<String,String> map1 = new HashMap<>();
        map1.put("max","299");
        map1.put("min","0");
        map1.put("name","range1");
        map1.put("remark","华东仓");
        rangeList.add(map1);

        Map<String,String> map2 = new HashMap<>();
        map2.put("max","999");
        map2.put("min","300");
        map2.put("name","range2");
        map2.put("remark","华南仓");
        rangeList.add(map2);

        Map<String,String> map3 = new HashMap<>();
        map3.put("max","2000");
        map3.put("min","1000");
        map3.put("name","range3");
        map3.put("remark","华西仓");
        rangeList.add(map3);

        Map<String,String> map4 = new HashMap<>();
        map4.put("max","5000");
        map4.put("min","2000");
        map4.put("name","range3");
        map4.put("remark","华北仓");
        rangeList.add(map4);

        indexVo.setRangeList(rangeList);

        // 缓存返回值
        redisCache.set(cacheKey, JSON.toJSONString(indexVo), cacheTime);

        return new BaseResponse(indexVo);
    }

/*    @ApiOperation(value = "搜索首页")
    @ApiResponses(@ApiResponse(code = 10000, response = SearchIndexVo.class, message = "content"))
    @RequestMapping("/searchIndex")
    public BaseResponse searchIndex() {
        SearchIndexVo searchIndexVo = new SearchIndexVo();

        List<SystemGroupData> hotKeyList = systemService.findSystemGroupData(SystemGroupEnum.ROUTINE_HOT_SEARCH.getKey());
        searchIndexVo.setHotKeyList(hotKeyList);

        return new BaseResponse(searchIndexVo);
    }*/



   /* @ApiOperation(value = "积分商城首页")
    @ApiResponses(@ApiResponse(code = 10000, response = IntegralIndexVo.class, message = "content"))
    @RequestMapping("/integralIndex")
    public BaseResponse integralIndex() {
        IntegralIndexVo indexVo = null;

        // 首页缓存key
        int cacheTime = 3600;  // 秒
        String cacheKey = "integral_index";
        // 缓存是否存在
        String cacheVal = redisCache.getString(cacheKey);
        if(StringUtils.isNotBlank(cacheVal)) {
            indexVo = JSON.parseObject(cacheVal, IntegralIndexVo.class);
            return new BaseResponse(indexVo);
        }

        indexVo = new IntegralIndexVo();

        // 热门商品列表
        List<StoreProductVo> hotIntegralList = storeProductService.list(6, StoreProductReq.hotIntegralList());

        indexVo.setIntegralhotList(hotIntegralList);

        // 缓存返回值
        redisCache.set(cacheKey, JSON.toJSONString(indexVo), cacheTime);

        return new BaseResponse(indexVo);
    }*/


}
